<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

require_once(APPPATH.'controllers/admin/applications.php');
class Master_data extends Applications
{
	public $master_data;
	public $module_table;

	public function __construct()
	{
		parent::__construct();

		/**
		 * @description check the session variables
		 */
		$this->M_login->session_check();

		/**
		 * @description the URL
		 */
		$this->bz_segment['END'] = 5;
		$this->bz_segment['MASTER_DATA'] = $this->uri->segment($this->bz_segment['END'], '');
		$this->bz_segment['URL'] = $this->M_functions->segment_create(FALSE, '/'.$this->bz_segment['MASTER_DATA']);
		$this->bz_segment['SITE_URL'] = site_url($this->bz_segment['URL']);

		/**
		 * @description access rights
		 */
		$this->access_rights = $this->M_menu->valid_check('db_default', $this->session->userdata('session_user_id'));
		$this->access_rights = array
			(
				'ADD' => in_array('add', $this->access_rights) ? TRUE : FALSE
				, 'EDIT' => in_array('edit', $this->access_rights) ? TRUE : FALSE
				, 'DELETE' => in_array('delete', $this->access_rights) ? TRUE : FALSE
			);

		/**
		 * @description initialize variables for database
		 */
		$module_table_id = $this->db->get_where('hris_master_module_table', array('name' => $this->bz_segment['MASTER_DATA']));
		($module_table_id->num_rows() > 0)
			? $this->module_table['ID'] = $module_table_id->row()->id
			: redirect('admin/desktop');

		/**
		 * @description hard code defined the type of masters!
		 */
		$this->master_data['TYPE'] = 'RECURSIVE';
		$this->master_data['TABLE_HEADER_VISIBLE'] = '';
		switch ($this->module_table['ID'])
		{
			case 22:
				/**
				 * @description load the models
				 */
				$this->load->model('admin/hris/HRIS_M_live_data');


				$this->master_data['REFERENCE'] = array
					(
						'LANGUAGE' => array
							(
								'TYPE' => $this->lang->line('hris_type')
							)
						, 'VIEW' => array
							(
								'FIELDS' => ', `fn_hris_master_data_get_name`("23", reference_id, "'.$this->session->userdata('session_user_language_id').'") as reference_name'
								, 'FIELDS_ORDER' => 'name'
							)
						, 'FORM' => array
							(
								'FIELDS' => ', reference_id'
								, 'COMBO_FILL' => $this->HRIS_M_live_data->combo_fill('db_default', 23)
							)
					);
				break;

			case 27:
				/**
				 * @description load the models
				 */
				$this->load->model('admin/hris/HRIS_M_live_data');


				$this->master_data['TYPE'] = 'NORMAL';
				$this->master_data['REFERENCE'] = array
					(
						'LANGUAGE' => array
							(
								'TYPE' => $this->lang->line('hris_type')
							)
						, 'VIEW' => array
							(
								'FIELDS' => ', `fn_hris_master_data_get_name`("42", reference_id, "'.$this->session->userdata('session_user_language_id').'") as reference_name'
								, 'FIELDS_ORDER' => 'name'
							)
						, 'FORM' => array
							(
								'FIELDS' => ', reference_id'
								, 'COMBO_FILL' => $this->HRIS_M_live_data->combo_fill('db_default', 42)
							)
					);
				break;

			case 29:
				/**
				 * @description load the models
				 * FIXME: which table that should be used as reference by the job_specification ?
				 */
				$this->load->model('admin/hris/HRIS_M_live_data');


				$this->master_data['REFERENCE'] = array
					(
						'LANGUAGE' => array
							(
								'TYPE' => $this->lang->line('hris_type')
							)
						, 'VIEW' => array
							(
								'FIELDS' => ', `fn_hris_master_data_get_name`("28", reference_id, "'.$this->session->userdata('session_user_language_id').'") as reference_name'
								, 'FIELDS_ORDER' => 'name'
							)
						, 'FORM' => array
							(
								'FIELDS' => ', reference_id'
								, 'COMBO_FILL' => $this->HRIS_M_live_data->combo_fill('db_default', 28)
							)
					);
				break;

			case 33:
				/**
				 * @description load the models
				 */
				$this->load->model('admin/hris/HRIS_M_live_data');


				$this->master_data['REFERENCE'] = array
					(
						'LANGUAGE' => array('TYPE' => NULL)
						, 'VIEW' => array('FIELDS' => '', 'FIELDS_ORDER' => 'id')
						, 'FORM' => array('FIELDS' => '', 'COMBO_FILL' => array())
					);
				break;

			default:
				/**
				 * @description normal type
				 */
				$this->master_data['TYPE'] = 'NORMAL';
				$this->master_data['TABLE_HEADER_VISIBLE'] = ' hidden';
				$this->master_data['REFERENCE'] = array
					(
						'LANGUAGE' => array('TYPE' => NULL)
						, 'VIEW' => array('FIELDS' => '', 'FIELDS_ORDER' => 'name')
						, 'FORM' => array('FIELDS' => '', 'COMBO_FILL' => '')
					);
				break;
		}


		$this->tbl_attr = array
			(
				'TABLE' => array
					(
						'NAME' => 'hris_master_data'
						, 'NAME_ALIAS' => 'hris_master_data a'
						, 'PRIMARY' => array
							(
								'SHOW' => FALSE
								, 'DATA' => array('id')
							)
					)
				, 'TABLE_MODAL' => array
					(
						'NAME' => 'hris_master_data_language'
						, 'NAME_ALIAS' => 'hris_master_data_language b'
						, 'PRIMARY' => array
							(
								'SHOW' => FALSE
								, 'DATA' => array('data__id', 'language__id')
							)
					)
				, 'VIEW' => array
					(
						'HEADING' => array
								(
									$this->lang->line('hris_name')
									, $this->lang->line('hris_description')
									, $this->master_data['REFERENCE']['LANGUAGE']['TYPE']
									, $this->access_rights['DELETE'] ? '&nbsp;' : NULL
								)
						, 'FIELDS' => 'id'
								. ', (select name from bz_hris_master_data_language c where a.id = c.data__id and language__id = "'.$this->session->userdata('session_user_language_id').'") as name'
								. ', (select description from bz_hris_master_data_language c where a.id = c.data__id and language__id = "'.$this->session->userdata('session_user_language_id').'") as description'
								. $this->master_data['REFERENCE']['VIEW']['FIELDS']
						, 'FIELDS_EDIT' => 'name'
						, 'FIELDS_ORDER' => $this->master_data['REFERENCE']['VIEW']['FIELDS_ORDER']
						, 'FIELDS_PARENT_NAME' => 'parent_id'
						, 'TOT_ROW' => $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_VIEW_TOT_ROW']
						, 'TABLE' => array
							(
								'WHERE' => array('module_table__id' => $this->module_table['ID'])
							)
					)
				, 'VIEW_DETAIL' => array
					(
						'HEADING' => array
							(
								$this->lang->line('fields_tools_language_name')
								, $this->lang->line('hris_name')
								, $this->lang->line('hris_description')
								, $this->access_rights['DELETE'] ? '&nbsp;' : NULL
							)
						, 'FIELDS' => 'data__id, language__id, (select name from bz_biskitz_language c where b.language__id = c.id) as language_name'
								. ', name, description'
						, 'FIELDS_EDIT' => 'name'
						, 'FIELDS_ORDER' => 'language_name, description'
					)
				, 'FORM' => array
					(
						'FIELDS' => 'id, module_table__id, parent_id'.$this->master_data['REFERENCE']['FORM']['FIELDS']
						, 'FIELDS_ORDER' => ''
						, 'COLUMN' => 4
					)
				, 'FORM_MODAL' => array
					(
						'HEADING' => $this->lang->line('hris_modal')
						, 'FIELDS' => 'data__id, language__id, (select name from bz_biskitz_language c where b.language__id = c.id) as language_name'
								. ', name, description'
						, 'FIELDS_ORDER' => ''
						, 'COLUMN' => 2
					)
				, 'URL' => array
					(
						'FORM' => $this->bz_segment['SITE_URL'].'/form'
						, 'VIEW' => $this->bz_segment['SITE_URL'].'/view'
					)
			);

		$this->tbl_viewdetail_attr = array
			(
				'NAME' => $this->tbl_attr['TABLE_MODAL']['NAME_ALIAS']
				, 'PRIMARY' => $this->tbl_attr['TABLE_MODAL']['PRIMARY']
				, 'HEADING' => $this->tbl_attr['VIEW_DETAIL']['HEADING']
				, 'FIELDS' => array
					(
						'NAME' => $this->tbl_attr['VIEW_DETAIL']['FIELDS']
						, 'CAPTION' => $this->tbl_attr['VIEW_DETAIL']['FIELDS_EDIT']
						, 'ORDER' => $this->tbl_attr['VIEW_DETAIL']['FIELDS_ORDER']
					)
				, 'JAVASCRIPT' => array
					(
						'VIEWLIST_GENERATE' => array
							(
								'JS_FUNCTION_NAME' => 'js_modaldialog_data_put'
								, 'JS_FUNCTION_PARAMETERS' => '"'.$this->bz_segment['SITE_URL'].'/ajx_live_form_detail", "frm_modal", "btn_add_detail", "txt72", "js_modal_functions", "'.$this->bz_segment['SITE_URL'].'", "ajx_modal_data_", "put"'
							)
					)
			);

		/**
		 * @description template lite variables
		 */
		$this->bz_tlite_var['CACHE_PATH'] = $this->bz_tlite_var['SESSION']['USER_ID'].'|'.str_replace('/', '|', $this->bz_segment['URL']);
		$this->bz_tlite_var['CACHE'] = array
			(
				'PUBLIC' => $this->bz_tlite_var['SESSION']['USER_ID']
				, 'PRIVATE' => array
					(
						'GLOBAL' => $this->bz_tlite_var['CACHE_PATH']
						, 'VIEW' => $this->bz_tlite_var['CACHE_PATH'].'|view'
						, 'FORM' => $this->bz_tlite_var['CACHE_PATH'].'|form'
					)
			);
	}


	/**
	 * @function _remap
	 * @description remap the module
	 */
	public function _remap($method, $parameters = array())
	{
		if (count($parameters) > 0)
		{
			$method = $parameters[0];
			$method_temp = array_shift($parameters);

			call_user_func_array(array($this, $method), $parameters);
		}
		else
		{
			$this->view();
		}
	}


	/**
	 * @function index()
	 * @description main controller
	 */
	public function index()
	{
		/**
		 * @description choose which controller to show
		 */
		redirect('admin/desktop');
	}


	/**
	 * @function view()
	 * @description view table list of data
	 */
	public function view($prm_page = '', $prm_search_term = '')
	{
		if ($this->web_data['BISKITZ']['HEADER']['BROWSER_SUPPORTED'] == '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('M_viewlist');
			$this->load->library('encrypt');


			if ($this->master_data['TYPE'] == 'NORMAL')
			{
				/**
				 * @description process the searching
				 */
				if (trim($this->input->post('txt_search')) != '')
				{
					$search_string = $this->encrypt->encode($this->input->post('txt_search'));
					redirect($this->bz_segment['SITE_URL'].'/view/0/'.$search_string);
				}
				if (trim($prm_search_term) != '')
				{
					$prm_search_term = strtolower($this->encrypt->decode($prm_search_term));
					$search_term = array
						(
							'lower(id)' => $prm_search_term
							/**
							 * FIXME: this should search the data; NOT the module_table__id! using sub query maybe??
							 */
							, 'lower(module_table__id)' => $prm_search_term
						);
				}
				else
				{
					$search_term = '';
				}
			}


			/**
			 * @description if there isn't any css/ajax cache, build it! else show cache!
			 */
			if ( ! $this->bz_tlite->is_cached($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['VIEW']))
			{
				/**
				 * @section begin page init
				 */
					/**
					 * @description show the menu
					 */
					$menu = $this->M_menu->shows();
					$this->web_data['JAVASCRIPT_MENU'] = $menu['JAVASCRIPT_MENU'];
					$this->web_data['PAGE']['MENU'] = $menu['PAGE']['MENU'];

					/**
					 * @description init the css and the ajax needed
					 */
					$css_files = array
						(
							array('layout' => 'screen')
							, array('tipsy/tipsy' => 'screen')
							, array('form_icon_bar' => 'screen')
							, array('nanoscroller/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
							, array('shortcut_bar' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
							, array('ddm/multi-ddm' => 'screen')
						);
					$js_files = array
						(
							'js/plugins/ddm' => array('jquery.multi-ddm.pack')
							, 'js/plugins/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? array('jquery.nanoscroller.min', 'overthrow.min') : NULL
							, 'js/plugins/tipsy' => array('jquery.tipsy.pack')
							, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_FUNCTIONS_PATH'] => array('common', 'form', 'error')
							, 'themes/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN'].'/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_PATH'] => array('common')
						);
				/**
				 * @section end page init
				 */


				/**
				 * @section begin generate template variables
				 */

					/**
					 * @description css and ajax
					 */
					$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
					$this->web_data['PAGE']['JAVASCRIPT_FILES'] = $this->M_template->js_generate($js_files);

					/**
					 * @description initialize the additional javascript
					 */
					if ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error')
					{
						$this->web_data['JAVASCRIPT'] .= '
							<script>
								$(function()
								{
									$(".nano").nanoScroller({preventPageScrolling: true});
								});
							</script>
							';
					}

					/**
					 * @description menu descriptions
					 */
					$this->web_data['PAGE']['DETAIL'] = $this->M_menu->detail();
				/**
				 * @section end generate template variables
				 */
			}


			/**
			 * @section begin table view init
			 */
				/**
				 * @description init the command button
				 */
				$this->load->model('admin/M_command_bar');


				switch ($this->master_data['TYPE'])
				{
					case 'NORMAL':
						/**
						 * @description init the pagination
						 */
						$paging_init = array
							(
								'base_url' => $this->tbl_attr['URL']['VIEW']
								, 'total_rows' => $this->M_database->total_rows
									(
										'db_default'
										, $this->tbl_attr['TABLE']['NAME']
										, $this->tbl_attr['VIEW']['TABLE']['WHERE']
										, FALSE
										, $search_term
									)
								, 'per_page' => $this->tbl_attr['VIEW']['TOT_ROW']
								, 'uri_segment' => $this->bz_segment['END'] + 2
							);

						$this->load->library(array('pagination'));
						$this->pagination->initialize($paging_init);
						$paging_link = $this->pagination->my_paging_links(TRUE, $this->bz_segment['END'] + 3);

						/**
						 * @description init the command button
						 */
						$view_list_commands = $this->M_command_bar->view($prm_search_term, $paging_link);

						/**
						 * @description init the table list
						 */
						$view_list_content = $this->M_viewlist->generate
							(
								'db_default'
								, $this->tbl_attr['VIEW']['FIELDS'], $this->tbl_attr['TABLE']['PRIMARY'], $this->tbl_attr['VIEW']['FIELDS_EDIT'], $this->tbl_attr['VIEW']['FIELDS_ORDER']
								, $this->tbl_attr['TABLE']['NAME_ALIAS'], $this->tbl_attr['VIEW']['TABLE']['WHERE']
								, $search_term
								, $this->bz_segment['URL']
								, $this->tbl_attr['VIEW']['TOT_ROW']
								, 'form'
								, $this->access_rights['EDIT']
								, $this->access_rights['DELETE']
							);
						break;


					case 'RECURSIVE':
						/**
						 * @description init the command button
						 */
						$view_list_commands = $this->M_command_bar->view('', '', FALSE);

						/**
						 * @description init the table list
						 */
						$view_list_content = $this->M_viewlist->generate
							(
								'db_default'
								, $this->tbl_attr['VIEW']['FIELDS'], $this->tbl_attr['TABLE']['PRIMARY'], $this->tbl_attr['VIEW']['FIELDS_EDIT'], $this->tbl_attr['VIEW']['FIELDS_ORDER']
								, $this->tbl_attr['TABLE']['NAME_ALIAS'], $this->tbl_attr['VIEW']['TABLE']['WHERE']
								, ''
								, $this->bz_segment['URL']
								, 0
								, 'form'
								, $this->access_rights['EDIT']
								, $this->access_rights['DELETE']
								, 'none'
								, FALSE
								, FALSE, ''
								, TRUE
								, $this->tbl_attr['VIEW']['FIELDS_PARENT_NAME']
							);
						break;
				}

				if (is_array($view_list_content))
				{
					$this->table->set_heading($this->tbl_attr['VIEW']['HEADING']);
					$view_list_content = $this->table->generate($view_list_content);
				}
				else
				{
					$view_list_content = '<center class="error_field">'.$this->lang->line('data_view_error').'</center>';
				}


			/**
			 * @section begin generate template variables
			 */

				/**
				 * @description view list
				 */
				$this->web_data['PAGE']['VIEW'] = array
					(
						'FORM' => array
							(
								'OPEN' => form_open($this->tbl_attr['URL']['VIEW'], array('id' => 'frm_header', 'onSubmit' => 'return false'), '')
							)
						, 'COMMANDS' => $view_list_commands
						, 'LISTS' => $view_list_content
					);
			/**
			 * @section end generate template variables
			 */


			/**
			 * @description log the action!
			 */
			switch ($this->master_data['TYPE'])
			{
				case 'NORMAL':
					$this->M_log->write
						(
							'db_default'
							, trim($prm_page) != ''
								? sprintf
									(
										trim($prm_search_term) != '' ? $this->lang->line('log_view_search') : $this->lang->line('log_view_page')
										, $this->router->class
										, $this->pagination->cur_page
										, $prm_search_term
									)
								: sprintf($this->lang->line('log_view_access'), $this->router->class)
						);
					break;


				case 'RECURSIVE':
					$this->M_log->write
						(
							'db_default'
							, sprintf($this->lang->line('log_view_access'), $this->router->class)
						);
					break;
			}



		}
		else
		{
			/**
			 * @section Browser NOT Supported
			 */
				/**
				 * @description browser not supported message error
				 */
				$css_files = array(array('layout' => 'screen'));


				/**
				 * @section begin generate template variables
				 */
					/**
					 * @description css and ajax
					 */
					$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
				/**
				 * @section end generate template variables
				 */
		}



		/**
		 * @description output template variables
		 */
		$this->bz_tlite->assign('WEB_DATA', $this->web_data);


		/**
		 * @section begin output
		 */
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['HEADER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['VIEW']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['MENU'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_DESCRIPTION'], $this->bz_tlite_var['CACHE']['PRIVATE']['GLOBAL']);

		$this->bz_tlite->cache = FALSE;
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_FORM_COMMAND_BAR']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_FORM']);

		$this->bz_tlite->cache = TRUE;
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_SHORTCUT_BAR'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['JAVASCRIPT'], $this->bz_tlite_var['CACHE']['PRIVATE']['VIEW']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER_END'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		/**
		 * @section end output
		 */
	}


	/**
	 * @function form()
	 * @description form for data entry
	 */
	public function form($prm_key = '')
	{
		if ($this->web_data['BISKITZ']['HEADER']['BROWSER_SUPPORTED'] == '')
		{
			/**
			 * @description if the user has ADD or EDIT access, show the form!
			 */
			if ($this->access_rights['ADD'] OR ($this->access_rights['EDIT'] && trim($prm_key) != ''))
			{
				/**
				 * @description if there isn't any css/ajax cache, build it! else show cache!
				 */
				if ( ! $this->bz_tlite->is_cached($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']))
				{
					/**
					 * @section begin page init
					 */
						/**
						 * @description show the menu
						 */
						$menu = $this->M_menu->shows();
						$this->web_data['JAVASCRIPT_MENU'] = $menu['JAVASCRIPT_MENU'];
						$this->web_data['PAGE']['MENU'] = $menu['PAGE']['MENU'];

						/**
						 * @description init the css and the ajax needed
						 */
						$css_files = array
							(
								array('layout' => 'screen')
								, array('tipsy/tipsy' => 'screen')
								, array('validate/validate' => 'screen')
								, array('form_icon_bar' => 'screen')
								, array('nanoscroller/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
								, array('shortcut_bar' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
								, array('jqModal/jqModal' => 'screen')
								, array('simplemodal/simplemodal' => 'screen')
								, array('autocomplete/autocomplete' => 'screen')
								, array('ddm/multi-ddm' => 'screen')
							);
						$js_files = array
							(
								'js/plugins/ddm' => array('jquery.multi-ddm.pack')
								, 'js/plugins/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? array('jquery.nanoscroller.min', 'overthrow.min') : NULL
								, 'js/plugins/validate' => array('jquery.validate.pack', 'additional-methods', 'jquery.metadata')
								, 'js/plugins/tipsy' => array('jquery.tipsy.pack')
								, 'js/plugins/jqModal' => array('jqModal.pack')
								, 'js/plugins/autocomplete' => array('jquery.autocomplete.pack')
								, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_FUNCTIONS_PATH'] => array('common', 'form', 'error', 'modaldialog', 'autocomplete')
								, 'themes/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN'].'/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_PATH'] => array('common')
							);
					/**
					 * @section end page init
					 */


					/**
					 * @section begin form initialize
					 */
						$shortcut_f8_action = '$(\'#btn_add_detail\').click();';
						/**
						 * @description initialize the form elements
						 */
						$form_content = form_input
							(
								array
								(
									'type' => 'hidden'
									, 'name' => 'txtinput[0]', 'id' => 'txt0'
									, 'size' => '15', 'maxlength' => '15'
									, 'readOnly' => 'readOnly', 'class' => 'read_only'
								)
								, ''
								, 'onFocus="javascript: $(\'#txt1\').focus().select();"'
							);

						switch ($this->master_data['TYPE'])
						{
							case 'RECURSIVE':
								$form_content = array
									(
										$form_content
										. '<b>'.$this->lang->line('hris_parent').'</b>'
										, form_input
											(
												array
												(
													'type' => 'hidden'
													, 'name' => 'txtinput[1]', 'id' => 'txt1'
													, 'size' => '15', 'maxlength' => '15'
													, 'readOnly' => 'readOnly', 'class' => 'read_only do_not_change'
												)
												, $this->module_table['ID']
												, 'onFocus="javascript: $(\'#txt2\').focus().select();"'
											)
											. form_dropdown
												(
													'txtinput[2]'
													, $this->HRIS_M_live_data->combo_fill
														(
															'db_default'
															, array('ID' => $this->module_table['ID'], 'NAME' => 'master_data')
															, '', 'RECURSIVE', array('module_table__id' => $this->module_table['ID'])
														)
													, ''
													, 'id="txt2" class="{validate: {required: true}}"'
														. ' shortcut_f2="'.$this->tbl_attr['URL']['VIEW'].'"'
														. ' shortcut_f8="'.$shortcut_f8_action.'"'
												)
									);


							case 'NORMAL':
								$form_content_normal = array
									(
										(
											( ! is_array($form_content))
												? $form_content
													. form_input
														(
															array
															(
																'type' => 'hidden'
																, 'name' => 'txtinput[1]', 'id' => 'txt1'
																, 'size' => '15', 'maxlength' => '15'
																, 'readOnly' => 'readOnly', 'class' => 'read_only do_not_change'
															)
															, $this->module_table['ID']
															, 'onFocus="javascript: $(\'#txt3\').focus().select();"'
														)
													. form_input
														(
															array
															(
																'type' => 'hidden'
																, 'name' => 'txtinput[2]', 'id' => 'txt2'
																, 'size' => '15', 'maxlength' => '15'
																, 'readOnly' => 'readOnly', 'class' => 'read_only do_not_change'
															)
															, '0'
															, 'onFocus="javascript: $(\'#txt3\').focus().select();"'
														)

												: ''
										)
										. (
												is_null($this->master_data['REFERENCE']['LANGUAGE']['TYPE'])
													? NULL
													: '<b>'.$this->master_data['REFERENCE']['LANGUAGE']['TYPE'].'</b>'
											)

										, (
												is_null($this->master_data['REFERENCE']['LANGUAGE']['TYPE'])
													? form_input
														(
															array
															(
																'type' => 'hidden'
																, 'name' => 'txtinput[3]', 'id' => 'txt3'
																, 'size' => '15', 'maxlength' => '15'
																, 'readOnly' => 'readOnly', 'class' => 'read_only'
															)
														)
													: form_dropdown
														(
															'txtinput[3]'
															, $this->master_data['REFERENCE']['FORM']['COMBO_FILL']
															, ''
															, 'id="txt3" class="{validate: {required: true}}"'
																. ' shortcut_f2="'.$this->tbl_attr['URL']['VIEW'].'"'
																. ' shortcut_f8="'.$shortcut_f8_action.'"'
														)
											)

										. (
												$this->access_rights['ADD']
													? form_button
														(
															array('id' => 'btn_add_detail', 'class' => 'input_default hidden')
															, $this->lang->line('button_add_detail')
														)
													: NULL
											)
										. form_submit
											(
												array('id' => 'btn_save', 'class' => 'input_not_default hidden')
												, $this->lang->line('button_save')
											)
									);

								if (is_array($form_content))
								{
									$form_content = array_merge($form_content, $form_content_normal);
								}
								else
								{
									$form_content = $form_content_normal;
								}
								break;
						}

						/**
						 * @description create the form and elements
						 */
						$this->table->clear();
						$table_template = array('table_open' => '<table class = "plain'.$this->master_data['TABLE_HEADER_VISIBLE'].'">');
						$this->table->set_template($table_template);

						$form_content = $this->table->make_columns($form_content, $this->tbl_attr['FORM']['COLUMN']);
						$form_content = $this->table->generate($form_content);
					/**
					 * @section end form initialize
					 */



					/**
					 * @section begin generate template variables
					 */

						/**
						 * @description css and ajax
						 */
						$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
						$this->web_data['PAGE']['JAVASCRIPT_FILES'] = $this->M_template->js_generate($js_files);

						/**
						 * @description initialize the additional javascript
						 */
						$this->web_data['JAVASCRIPT'] .= '
							<script>
								var js_modal_functions =
								"
									$(\"#btn_modal_save\").click
										(function()
										{
											js_form_action_validate
											(
												\"'.$this->bz_segment['SITE_URL'].'\", \"ajx_modal_\", \"insert\", \"\"
												, \"#frm_modal\", \"#process_modaldialog\", \"#error_message_modaldialog\"
												, \"validate\"
											);

											$(\"#txt72\").focus().select();
										});

									js_autocomplete_popup
									(
										\"#txt72\"
										, \"'.$this->bz_segment['SITE_URL'].'\", \"ajx_live_language_\", \"autocomplete/modal\", \"\"
										, \"ajx_live_language_\", \"autocomplete_select\", \"/71/modal\"
										, \"#frm_modal\", \"#process_modaldialog\", \"#error_message_modaldialog\"
									);
								";



								$(function()
								{
									$("#btn_add_detail").click
										(function()
										{
											js_modaldialog_show
											(
												"'.$this->bz_segment['SITE_URL'].'/ajx_live_form_detail/" + $("#txt0").val()
												, "frm_modal"
												, "btn_add_detail", "txt72"
												, js_modal_functions
											);
										});

									$("#btn_save").click
										(function()
										{
											js_form_action_validate
											(
												"'.$this->bz_segment['SITE_URL'].'", "ajx_", "insert", ""
												, "#frm_header", "#process", "#error_message"
												, "validate"
											);
										});

									$("#txt2").focus().select();
						';

						if ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error')
						{
							$this->web_data['JAVASCRIPT'] .= '$(".nano").nanoScroller({preventPageScrolling: true});';
						}

						$this->web_data['JAVASCRIPT'] .= '
								});
							</script>
						';

						/**
						 * @description menu descriptions
						 */
						$this->web_data['PAGE']['DETAIL'] = $this->M_menu->detail();

						/**
						 * @description icon bar
						 */
						$this->load->model('admin/M_command_bar');
						$this->web_data['PAGE']['ICON_BAR'] = $this->M_command_bar->form('TRUE', FALSE
						, $this->master_data['TYPE'] == 'RECURSIVE'
							OR ($this->master_data['TYPE'] == 'NORMAL' && ! is_null($this->master_data['REFERENCE']['LANGUAGE']['TYPE']))
							? TRUE : FALSE
						, TRUE);

						/**
						 * @description form dialogs
						 */
						$this->web_data['PAGE']['FORM'] = array
							(
								'OPEN' => form_open('', array('id' => 'frm_header', 'onSubmit' => 'return false'), '')
								, 'CONTENT' => $form_content
								, 'CENTER' => 'FALSE'
							);

						/**
						 * @description notes
						 */
						$this->web_data['PAGE']['NOTES'] = array
							(
								'USE' => 'TRUE'
								, 'MESSAGE' => array
									(
										'SHORTCUTS' => $this->lang->line('notes_shortcuts')
										, 'ADDITIONAL' => $this->lang->line('notes_additional_modaldialog')
									)
							);
					/**
					 * @section end generate template variables
					 */
				}



				/**
				 * @description get the data for editing via ajax
				 */
				if ($this->access_rights['EDIT'] && trim($prm_key) != '')
				{
					$this->web_data['JAVASCRIPT_EXTEND'] = '
						<script>
							$(function()
							{
								js_form_action
								(
									"'.$this->bz_segment['SITE_URL'].'", "ajx_data_", "put", "'.$prm_key.'"
									, "#frm_header", "#process", "#error_message"
								);
							});
						</script>
					';
				}


				/**
				 * @description log the action!
				 */
				$this->M_log->write('db_default', sprintf($this->lang->line('log_form_access'), $this->router->class, 'form'));
			}
			else
			{
				/**
				 * @description doesn't have ADD or EDIT access? redirect to desktop
				 */
				redirect('admin/desktop');
			}



		}
		else
		{
			/**
			 * @section Browser NOT Supported
			 */
				/**
				 * @description browser not supported message error
				 */
				$css_files = array(array('layout' => 'screen'));


				/**
				 * @section begin generate template variables
				 */
					/**
					 * @description css and ajax
					 */
					$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
				/**
				 * @section end generate template variables
				 */
		}



		/**
		 * @description output template variables
		 */
		$this->bz_tlite->assign('WEB_DATA', $this->web_data);


		/**
		 * @section begin output
		 */
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['HEADER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['MENU'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_DESCRIPTION'], $this->bz_tlite_var['CACHE']['PRIVATE']['GLOBAL']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_FORM_ICON_BAR'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_FORM'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_SHORTCUT_BAR'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['JAVASCRIPT'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);

		$this->bz_tlite->cache = FALSE;
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['JAVASCRIPT_EXTEND']);

		$this->bz_tlite->cache = TRUE;
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER_END'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		/**
		 * @section end output
		 */
	}



/**
 * @section AJAX Action Section
 */

	/**
	 * @function ajx_insert()
	 * @description insert the data
	 */
	public function ajx_insert()
	{
		if ($this->input->is_ajax_request() && ($this->access_rights['ADD'] OR $this->access_rights['EDIT']))
		{
			if ($this->input->post('txtinput[0]') != $this->input->post('txtinput[2]'))
			{
				/**
				 * @description load the models
				 */
				$this->load->model('admin/M_live_data');


				$return_value = $this->M_live_data->insert
					(
						'db_default'
						, $this->tbl_attr['TABLE']['NAME']
						, array('id' => $this->input->post('txtinput[0]'))
						, $this->input->post('txtinput')
						, 'form'

						, TRUE, $this->tbl_attr['URL']['VIEW']
					);

				if ($return_value[0] == 'js_action_update')
				{
					$return_value = array_merge($return_value, array($this->access_rights));
				}
			}
			else
			{
				$return_value = array
					(
						'js_action_message_not_clear'
						, array($this->lang->line('hris_msg_parent_same_level_error'), 'error')
					);
			}


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_delete()
	 * @description delete the data
	 */
	public function ajx_delete($prm_key = '')
	{
		if ($this->input->is_ajax_request() && $this->access_rights['DELETE'])
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');


			$return_value = $this->M_live_data->delete
				(
					'db_default'
					, $this->tbl_attr['TABLE']['NAME']
					, $prm_key
					, array($this->input->post('txtinput[0]'))
					, array('id')
				);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_data_put()
	 * @description get the data from database and put them to the form via ajax!
	 */
	public function ajx_data_put($prm_key = '')
	{
		if ($this->input->is_ajax_request() && $this->access_rights['EDIT'] && trim($prm_key) != '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');
			/**
			 * @description load the helper
			 */
			$this->load->helper('viewlist');


			$return_value = $this->M_live_data->put
				(
					'db_default'
					, $this->tbl_attr['TABLE']['NAME']
					, array('id' => $prm_key)
					, $this->tbl_attr['FORM']['FIELDS']
					, 0, 'log_form_edit', 'form'

					, 'js_action_data_detail'
					, array
						(
							'FUNCTION_NAME' => 'detail_generate'
							, 'FUNCTION_ATTR' => array
								(
									'db_default'
									, $this->tbl_viewdetail_attr
									, array('data__id' => $prm_key)
									, $this->tbl_viewdetail_attr['JAVASCRIPT']['VIEWLIST_GENERATE']
								)
						)
				);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}



/**
 * @section AJAX MODAL Action Section
 */

	/**
	 * @function ajx_modal_data_put()
	 * @description get the data from database and put them to the form via ajax!
	 */
	public function ajx_modal_data_put($prm_modules_id = '', $prm_language_id = '')
	{
		if ($this->input->is_ajax_request() && $this->access_rights['EDIT'] && trim($prm_modules_id) != '' && trim($prm_language_id) != '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');


			$return_value = $this->M_live_data->put
				(
					'db_default'
					, $this->tbl_attr['TABLE_MODAL']['NAME_ALIAS']
					, array('data__id' => $prm_modules_id, 'language__id' => $prm_language_id)
					, $this->tbl_attr['FORM_MODAL']['FIELDS']
					, 70
					, 'log_form_edit'
					, 'modal'
				);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_modal_insert()
	 * @description insert the detail data
	 */
	public function ajx_modal_insert()
	{
		if ($this->input->is_ajax_request() && ($this->access_rights['ADD'] OR $this->access_rights['EDIT']))
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');
			/**
			 * @description load the helper
			 */
			$this->load->helper('viewlist');


			/**
			 * @description process the header
			 */
			$header = array
				(
					'ID' => $this->input->post('txtinput[70]')
					, 'KEY' => array('id' => $this->input->post('txtinput[70]'))
					, 'DATA' => array('id' => NULL, 'module_table__id' => $this->module_table['ID'])
				);

			$header_return = $this->M_live_data->insert
				(
					'db_default'
					, $this->tbl_attr['TABLE']['NAME']
					, $header['KEY']
					, $header['DATA']
					, 'form'
					, FALSE, ''
					, FALSE
				);
			if ($header_return[0] != 'js_action_update')
			{
				$header['ID'] = $this->M_database->max_id('db_default', $this->tbl_attr['TABLE']['NAME'], 'id');
			}



			/**
			 * @description process the detail
			 */
			$detail = array
				(
					'KEY' => array('data__id' => $header['ID'], 'language__id' => $this->input->post('txtinput[71]'))
					, 'DATA' => array
						(
							'data__id' => $header['ID']
							, 'language__id' => $this->input->post('txtinput[71]')
							, 'name' => $this->input->post('txtinput[73]')
							, 'description' => $this->input->post('txtinput[74]')
						)
				);

			$detail_return = $this->M_live_data->insert
				(
					'db_default'
					, $this->tbl_attr['TABLE_MODAL']['NAME']
					, $detail['KEY']
					, $detail['DATA']
					, 'modal'
					, TRUE, ''
					, FALSE, FALSE
				);
			if ($detail_return[0] != 'js_action_success' && $this->access_rights['ADD'])
			{
				$modaldialog_status = '';
			}
			else
			{
				$modaldialog_status = 'modaldialog_close_auto';
			}



			/**
			 * @description generate the viewlist live!
			 */
			$return_value = array
				(
					'js_action_modal_insert'
					, $detail_return[1], array($header['ID'])

					/**
					 * @description the header id in the MODAL form, use it to sync with the main form
					 */
					, 70

					/**
					 * @description put the header data to main form after success inserting detail data
					 */
					, array('#txt0', $header['ID'])

					/**
					 * @description generate the detail view list, LIVE!
					 */
					, array
						(
							'content_replace'
							, detail_generate
								(
									'db_default'
									, $this->tbl_viewdetail_attr
									, array('data__id' => $header['ID'])
									, $this->tbl_viewdetail_attr['JAVASCRIPT']['VIEWLIST_GENERATE']
								)
						)

					/**
					 * @description trigger the dialog to be automatically closed
					 */
					, array($modaldialog_status)
				);



			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_modal_delete()
	 * @description delete the detail data
	 */
	public function ajx_modal_delete($prm_modules_id = '', $prm_language_id = '', $prm_return_type = 'boolean')
	{
		if ($this->input->is_ajax_request() && $this->access_rights['DELETE'] && trim($prm_modules_id) != '' && trim($prm_language_id) != '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');


			$return_value = $this->M_live_data->delete
				(
					'db_default'
					, $this->tbl_attr['TABLE_MODAL']['NAME']
					, $prm_modules_id.'/'.$prm_language_id
					, array()
					, array('data__id', 'language__id')
					, 'modal'
					, FALSE
					, $prm_return_type
				);

			switch ($return_value)
			{
				case 'DELETE_SUCCESS':
					/**
					 * @description load the helper
					 */
					$this->load->helper('viewlist');


					$return_value = array
						(
							'js_action_content_replace'
							, array
								(
									'content_replace'
									, detail_generate
										(
											'db_default'
											, $this->tbl_viewdetail_attr
											, array('data__id' => $prm_modules_id)
											, $this->tbl_viewdetail_attr['JAVASCRIPT']['VIEWLIST_GENERATE']
										)
								)
						);
					break;
			}


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}



/**
 * @section AJAX Live Section
 */

	/**
	 * @function ajx_live_form_detail
	 * @description generate the detail form via ajax
	 */
	public function ajx_live_form_detail($prm_key = '')
	{
		if ($this->input->is_ajax_request() && ($this->access_rights['ADD'] OR $this->access_rights['EDIT']))
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_layout');


			/**
			 * @section begin form MODAL initalize
			 */
				/**
				 * @description create the MODAL form and elements
				 */
				$form_modal_content = array
					(
						'<b>'.$this->lang->line('fields_tools_language_name').'</b><br /><small>'.$this->lang->line('fields_required').'</small>'
						, form_input
							(
								array
								(
									'type' => 'hidden'
									, 'name' => 'txtinput[70]', 'id' => 'txt70'
									, 'size' => '5', 'maxlength' => '5'
									, 'readOnly' => 'readOnly', 'class' => 'read_only'
								)
								, $prm_key
							)
							. form_input
								(
									array
									(
										'type' => 'hidden'
										, 'name' => 'txtinput[71]', 'id' => 'txt71'
										, 'size' => '3', 'maxlength' => '3'
										, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} read_only'
									)
								)
							. form_input
								(
									array
									(
										'name' => 'txtinput[72]', 'id' => 'txt72'
										, 'size' => '30', 'maxlength' => '30'
									)
								)
							. '<br /><small><i>'.$this->lang->line('fields_acomplete_note').'</i></small>'


						, '<b>'.$this->lang->line('hris_name').'</b><br /><small>'.$this->lang->line('fields_required').'</small>'
						, form_input
							(
								array
								(
									'name' => 'txtinput[73]', 'id' => 'txt73'
									, 'size' => '40', 'maxlength' => '255'
									, 'class' => '{validate: {required: true}}'
								)
							)


						, '<b>'.$this->lang->line('hris_description').'</b>'
						, form_input
							(
								array
								(
									'name' => 'txtinput[74]', 'id' => 'txt74'
									, 'size' => '50', 'maxlength' => '255'
								)
							)


						, '&nbsp;'
						, form_submit
							(
								array('id' => 'btn_modal_save')
								, $this->lang->line('button_save')
							)
					);
				/**
				 * @description create the form MODAL and elements
				 */
				$this->table->clear();
				$form_modal_content = $this->table->make_columns($form_modal_content, $this->tbl_attr['FORM_MODAL']['COLUMN']);
				$form_modal_content = $this->table->generate($form_modal_content);
			/**
			 * @section end form MODAL initalize
			 */


			/**
			 * @section begin generate template variables
			 */
			$return_value = array
				(
					/**
					 * @description form modal dialogs
					 */
					'FORM_MODAL' => array
						(
							'OPEN' => form_open('', array('id' => 'frm_modal', 'onSubmit' => 'return false'), '')
							, 'TITLE' => $this->tbl_attr['FORM_MODAL']['HEADING']
							, 'CONTENT' => $form_modal_content
						)

					/**
					 * @description notes modaldialog
					 */
					, 'NOTES_MODAL' => array
						(
							'USE' => 'TRUE'
							, 'MESSAGE' => array
								(
									'SHORTCUTS' => $this->lang->line('notes_modaldialog_shortcuts')
									, 'ADDITIONAL' => ''
								)
						)
				);
			/**
			 * @section end generate template variables
			 */


			$return_value = $this->M_layout->modaldialog_generate($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_live_language_autocomplete()
	 * @description pop up the autocomplete
	 */
	public function ajx_live_language_autocomplete($prm_log_type = 'form')
	{
		if ($this->input->is_ajax_request() && trim($this->input->post('q')) != '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');


			$return_value = $this->M_live_data->autocomplete('db_default', 'biskitz_language', array('id' => $this->input->post('q'), 'name' => $this->input->post('q')), $prm_log_type);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_live_language_autocomplete_select()
	 * @description get the data from database and put them to the form via ajax!
	 */
	public function ajx_live_language_autocomplete_select($prm_key = '', $prm_object_start = '0', $prm_log_type = 'form', $prm_log_lang = 'log_form_autocomplete_select')
	{
		if ($this->input->is_ajax_request() && trim($prm_key) != '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');


			$return_value = $this->M_live_data->put('db_default', 'biskitz_language', array('id' => $prm_key), 'id, name', $prm_object_start, $prm_log_lang, $prm_log_type);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}



}

/** End of file master_data.php */
/** Location: ./biskitz_mpc/biskitz/controllers/admin/hris/configuration/master_data.php */
